package com.xuexiang.xuidemo.slice.progress;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import com.xuexiang.xui_lib.component.progress.ratingbar.RatingBar;
import com.xuexiang.xui_lib.component.progress.ratingbar.RotationRatingBar;
import com.xuexiang.xui_lib.component.progress.ratingbar.ScaleRatingBar;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.slice.BaseAbilitySlice;
import com.xuexiang.xuidemo.util.ToastUtil;

public class RatingBarSlice extends BaseAbilitySlice {

    RatingBar ratingBar;
    ScaleRatingBar scaleRatingBar;
    RotationRatingBar rrbCustom;
    long lastTime;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_slice_ratingbarview;
    }

    @Override
    protected void initComponents() {
        super.initComponents();
        initView();
    }

    public void initView() {
        ratingBar = (RatingBar) findComponentById(ResourceTable.Id_rating_bar);
        scaleRatingBar = (ScaleRatingBar) findComponentById(ResourceTable.Id_scale_rating_bar);
        rrbCustom = (RotationRatingBar) findComponentById(ResourceTable.Id_rrb_custom);

        ratingBar.setOnRatingChangeListener((ratingBar, rating) -> {
            new EventHandler(EventRunner.getMainEventRunner()).postTask(()->{
                ToastUtil.showToast(getContext(), "当前星级：" + rating);
            },1000);
        });
        scaleRatingBar.setOnRatingChangeListener((ratingBar, rating) -> {
        });

        findComponentById(ResourceTable.Id_btn_add_rating).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (onDoubClick()) {
                    float currentRating = ratingBar.getRating();
                    ratingBar.setRating(currentRating + 0.25f);

                    currentRating = scaleRatingBar.getRating();
                    scaleRatingBar.setRating(currentRating + 0.25f);

                    currentRating = rrbCustom.getRating();
                    rrbCustom.setRating(currentRating + 0.25f);
                }
            }
        });
        findComponentById(ResourceTable.Id_slice_arcloadingview_backImage).setClickedListener(comp->terminate());
    }

    private boolean onDoubClick() {
        boolean isFlag = false;
        long time = System.currentTimeMillis() - lastTime;

        if (time > 1500) {
            isFlag = true;
            lastTime = System.currentTimeMillis();
        }
        return isFlag;
    }
}
